{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与编程语言一样，Numpy中的ndarray同样支持比较与排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.array([1,2,3])\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1. , 2. , 3.3],\n",
       "       [4. , 5.2, 6.8]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = np.array([[1. , 2. , 3.3],\n",
    "        [4. , 5.2, 6.8]])\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True, False],\n",
       "       [False, False, False]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool_array = a1 >= a2\n",
    "bool_array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有a1、a2两个数组，a1是一维的，a2是二维的，当我们判断a1≥a2时，得到的结果仍然是一个数组，通过观察我们可以发现，数组比较的方式就是用其中的每一个元素分别比较。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 6, 7, 5],\n",
       "       [2, 6, 5, 5, 4],\n",
       "       [7, 7, 7, 8, 9]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_array = np.random.randint(10, size=(3, 5))\n",
    "random_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 6, 7, 7],\n",
       "       [2, 4, 5, 5, 6],\n",
       "       [7, 7, 7, 8, 9]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(random_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "比较还是比较简单的，我们接着看排序，假如我们创建了一个size=(3, 5)的随机数组，现在要对其进行排序。通过观察我们可以发现，np.sort方法会对数组中每一个集合中的元素排序，[5, 8, 7, 1, 7]经过排序变成了[1, 5, 7, 7, 8]，以此类推。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 2, 0, 3],\n",
       "       [0, 4, 2, 3, 1],\n",
       "       [0, 1, 2, 3, 4]], dtype=int64)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(random_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除了np.sort方法外，np.argsort也是用来排序的，上面的调用结果看上去有点乱，第一行是[3, 0, 2, 4, 1]，其实这个是排序后原数组对应元素的索引值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原来的[5, 8, 7, 1, 7]，排序后的索引是[3, 0, 2, 4, 1]，其中索引0→8、1→7、2→7、3→5、4→1，索引指向的数组元素，正是原数组按从大到小排序后的索引值。"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "4eaf1be304415beee96765ae99c3f893cc8312c7f1196698e6029668e9aeb3e5"
  },
  "kernelspec": {
   "display_name": "Python 3.10.0 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
